package com.quanyan.stadium.web;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.stadium.api.req.ReqCreateArrangeLiveGameEvent;
import com.quanyan.stadium.api.req.ReqId;
import com.quanyan.stadium.api.req.v2.ReqGameEventOrder;
import com.quanyan.stadium.api.resp.RespUserGradeIntegralList;
import com.quanyan.stadium.biz.redis.DefaultConfigCacheDao;
import com.quanyan.stadium.constants.GameConstants;
import com.quanyan.stadium.entity.vo.GradeIntegralVo;
import com.quanyan.stadium.service.GameEventService;
import com.quanyan.stadium.service.GradeIntegralService;
import com.quanyan.stadium.service.commonComponent.OfficialGameCache;
import com.quanyan.stadium.service.gameEvent.LiveArrangeGameEventService;
import com.quanyan.stadium.service.gameEventOrder.GameEventOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/stadium/demo")
public class DemoController {

    @Autowired
    private OfficialGameCache officialGameCache;

    @Autowired
    private DefaultConfigCacheDao defaultConfigCacheDao;

    @Autowired
    private GameEventService gameEventService;

    @Autowired
    private LiveArrangeGameEventService liveArrangeGameEventService;

    @Autowired
    private GameEventOrderService gameEventOrderService;

    /**
     * 清除赛事对象从缓存
     *
     * */
    @RequestMapping(value = "/delObject1", method = RequestMethod.POST)
    public APIResponse delObject1(@RequestBody Map<String, Integer> map) {
        officialGameCache.delGameEvent(map.get("id"));
        return APIResponse.returnSuccess();
    }

    /**
     * 清除轮次对象从缓存
     *
     * */
    @RequestMapping(value = "/delObject2", method = RequestMethod.POST)
    public APIResponse delObject2(@RequestBody Map<String, Integer> map) {
        officialGameCache.delGameRound(map.get("id"));
        return APIResponse.returnSuccess();
    }

    /**
     * 清除赛程对象从缓存
     *
     * */
    @RequestMapping(value = "/delObject3", method = RequestMethod.POST)
    public APIResponse delObject3(@RequestBody Map<String, Integer> map) {
        officialGameCache.delGameSchedule(map.get("id"));
        return APIResponse.returnSuccess();
    }

    /**
     * 清除默认信息
     *
     * */
    @RequestMapping(value = "/delObject4", method = RequestMethod.POST)
    public APIResponse delObject4(@RequestBody Map<String, Integer> map) {
        defaultConfigCacheDao.delTbGameCommonParameterListMsgFromCache(GameConstants.STADIUM_DEFAULT_GROUP_PARAMETER_KEY);
        defaultConfigCacheDao.delTbGameCommonParameterListMsgFromCache(GameConstants.STADIUM_DEFAULT_GROUP_BASE_CONFIG_KEY);
        return APIResponse.returnSuccess();
    }

    /**
     * 清楚赛事筛选项
     *
     * */
    @RequestMapping(value = "/delObject5", method = RequestMethod.POST)
    public APIResponse delObject5(@RequestBody Map<String, Integer> map) {
        officialGameCache.delGameEventConfig(map.get("id"));
        return APIResponse.returnSuccess();
    }

    @Autowired
    private GradeIntegralService gradeIntegralService;

    @RequestMapping(value = "/getGradeIntegralExplain",method = RequestMethod.POST)
    public APIResponse<List<GradeIntegralVo>> getGradeIntegralExplain(@RequestBody ReqId req){
        List<Integer> categoryIds = new ArrayList<>();
        if(null != req.getCategoryId()){
            categoryIds.add(req.getCategoryId());
        }else{
            categoryIds.add(GameConstants.CATEGORY_ID_TENNIS);
            categoryIds.add(GameConstants.CATEGORY_ID_BADMINTON);
        }
        return APIResponse.returnSuccess(gradeIntegralService.getGradeIntegralExplain(categoryIds));
    }

    @RequestMapping(value = "/getUserGradeIntegral",method = RequestMethod.POST)
    public APIResponse<RespUserGradeIntegralList> getUserGradeIntegral(@RequestBody ReqId req){
        Integer uid = req.getUid();
        List<Integer> categoryIds = new ArrayList<>();
        if(null != req.getCategoryId()){
            categoryIds.add(req.getCategoryId());
        }else{
            categoryIds.add(GameConstants.CATEGORY_ID_TENNIS);
            categoryIds.add(GameConstants.CATEGORY_ID_BADMINTON);
        }
        return APIResponse.returnSuccess(gradeIntegralService.getUserGradeIntegral(uid,categoryIds));
    }

    @RequestMapping(value = "/evalOwnFLScore",method = RequestMethod.POST)
    public APIResponse evalOwnFLScore(@RequestBody ReqId req){
        Integer uid = req.getUid();
        if(null == req.getCategoryId() || null == req.getScore()){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        try {
            gradeIntegralService.evalOwnFLScore(uid,req.getScore(),req.getCategoryId());
        }catch (Exception e){
            return APIResponse.returnFail("");
        }
        return APIResponse.returnSuccess();
    }

    /**
     * 现场编排 —— 获取分项、赛制、等级
     */
    @RequestMapping(value="/gameCategoryAndSystemForLive", method = RequestMethod.POST)
    public APIResponse<?> gameCategoryAndSystemForLive(@RequestBody ReqId reqId, BindingResult result) {

        if(null == reqId.getCategoryId()){
            return APIResponse.returnFail("categoryId不能为空");
        }
        if(null == reqId.getGameType()){
            return APIResponse.returnFail("gameType不能为空");
        }

        return APIResponse.returnSuccess(gameEventService.getGameSystemAndUserClubMsg(reqId.getCategoryId(), reqId.getUid()));
    }

    /**
     * 小球现场编排 —— 创建现场编排
     */
    @RequestMapping(value="/createArrangeLiveGameEvent", method = RequestMethod.POST)
    public APIResponse<Integer> createArrangeLiveGameEvent(@RequestBody ReqCreateArrangeLiveGameEvent req,BindingResult result) throws Exception{
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        Integer uid = 864;
        return APIResponse.returnSuccess(liveArrangeGameEventService.createGameEvent(uid, req).getId());
    }

    /**
     * 小球现场编排 —— 添加黑名单
     */
    @RequestMapping(value="/insertBlackUid", method = RequestMethod.POST)
    public APIResponse<Integer> insertBlackUid(@RequestBody Map<String, Integer> map) {
        officialGameCache.updateBlackUser(map.get("uid"));
        return APIResponse.returnSuccess();
    }

    /**
     * 小球现场编排 —— 删除黑名单
     */
    @RequestMapping(value="/deleteBlackUid", method = RequestMethod.POST)
    public APIResponse<Integer> createArrangeLiveGameEvent(@RequestBody Map<String, Integer> map) {
        officialGameCache.deleteOneBlackUser(map.get("uid"));
        return APIResponse.returnSuccess();
    }

    /**
     * 删除内部赛筛选项配置
     */
    @RequestMapping(value="/deleteClubGameEventConfig", method = RequestMethod.POST)
    public APIResponse<Integer> deleteClubGameEventConfig() {
        officialGameCache.deleteClubGameEventConfig();
        return APIResponse.returnSuccess();
    }

    /**
     *
     * 赛事订单列表
     *
     */
    @RequestMapping(value="/getGameEventOrderList", method = RequestMethod.POST)
    public APIResponse<?> gameCategoryAndSystem(@RequestBody ReqGameEventOrder reqGameEventOrder, HttpServletRequest request,BindingResult result)  {
        reqGameEventOrder.setUid(reqGameEventOrder.getUid());
        return APIResponse.returnSuccess(PageObj.create(0, reqGameEventOrder.getPageNo(), reqGameEventOrder.getPageSize(), gameEventOrderService.getGameEventOrderList(reqGameEventOrder)));
    }

    /**
     * 赛事编辑----赛事完结(用于处理特殊情况)
     *
     * 用户操作失误，需要把参赛费用退到组织者手里
     * */
    @RequestMapping(value="/finishGameEvent", method = RequestMethod.POST)
    public APIResponse<?> finishGameEvent(@RequestBody ReqId reqId, BindingResult result) throws InvocationTargetException, IllegalAccessException {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return gameEventService.finishGameEvent(reqId.getGameEventId());
    }

}
